package guilddao

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"

	"xr-game-server/core/cache"
	"xr-game-server/entity"
)

var guildCache *cache.CacheMgr

func initGuild() {
	guildCache = cache.NewCacheMgr()
}

// GetGuild 获取公会信息
func GetGuild(guildId uint64) *entity.Guild {
	// 先从缓存获取
	cacheData := guildCache.GetData(guildId, func(ctx context.Context) (value interface{}, err error) {
		var guild *entity.Guild
		g.DB().Model(string(entity.TbGuild)).Unscoped().WherePri(guildId).Scan(&guild)
		if guild == nil {
			return entity.NewGuild(), nil
		}
		return guild, nil
	})
	return cacheData.(*entity.Guild)
}
